package com.demo.webapp.action.root;

import java.util.List;

import com.demo.core.model.Function;
import com.demo.core.model.Site;
import com.demo.core.model.SiteGroup;
import com.demo.core.service.base.FunctionService;
import com.demo.core.service.base.SiteFunctionService;
import com.demo.core.service.base.SiteGroupService;
import com.demo.core.service.base.SiteService;
import com.demo.webapp.action.PageAction;
import com.demo.webapp.cache.model.WebCacheModel;
import com.demo.webapp.context.MultisiteContextHolder;
import com.demo.webapp.filter.MultisiteFilter;

/**
 * 站点管理Action
 * @author ryuu.kk
 *
 */
public class SiteAction extends PageAction {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -2704331427569780693L;

	/**
	 * 主键ID
	 */
	private Long id;
	
	/**
	 * 站点模型
	 */
	private Site site;
	
	/**
	 * 站点服务
	 */
	private SiteService siteService;
	
	/**
	 * 站点列表
	 */
	private List<Site> siteList;
	
	/**
	 * 站点分组模型
	 */
	private List<SiteGroup> siteGroupList;
	
	/**
	 * 站点分组服务
	 */
	private SiteGroupService siteGroupService;
	
	/**
	 * 功能组件服务
	 */
	private FunctionService functionService;
	
	/**
	 * 站点功能模型服务
	 */
	private SiteFunctionService siteFunctionService;
	
	/**
	 * url缓存模型列表
	 * 缓存在Action中
	 */
	private List<WebCacheModel> webCacheModelList;
	
	/**
	 * 站点列表
	 * @return String
	 */
	public String list() {
		// 1.站点信息
		siteList = siteService.findAll();
		return SUCCESS;
	}
	
	/**
	 * 创建站点
	 * @return String
	 */
	public String create() {
		
		// 取得站点类型
		siteGroupList = siteGroupService.findAll();
		return SUCCESS;
	}
	
	/**
	 * 站点信息维护-保存
	 * @return String
	 */
	public String save() {
		// 取得站点ID
		//Long siteId = MultisiteContextHolder.getSite().getId();
		// 新建后,站点初始化为关闭状态
		site.setIsClose(true);
		id = siteService.save(site);
		
		Long groupId = site.getSiteGroupId();
		// 为站点分配指定功能(只分配,不授权)
		List<Function> functionList = functionService.findList(groupId);
		// 为站点分配功能(但不可用).由站点功能分配分配功能
		siteFunctionService.saveAll(id, false, functionList);
		addActionMessage("站点信息已创建.");
				
		return CALLBACK;
	}
	
	/**
	 * 站点信息维护
	 */
	public String edit() {
		
		if (id == null) {
			// 站点ID为空,使用当前站点ID
			site = MultisiteContextHolder.getSite();
			id = site.getId();
		}
		this.site = siteService.find(id);
		this.siteGroupList = siteGroupService.findAll();
		
		return SUCCESS;
	}

	/**
	 * 站点信息维护-更新
	 * @return String
	 */
	public String update() {
		// 取得站点ID
		//Long siteId = MultisiteContextHolder.getSite().getId();
		if (siteService.exists(id)) {
			// 更新站点信息
			siteService.update(id, site);
			// 更新站点持有者信息
			siteService.updateOwner(id, site);
			// 刷新多站点模型缓存
			MultisiteFilter.refresh();
			addActionMessage("站点信息已更新.");
		} else {
			addActionError("站点不存在,无法更新");
		}
		
		return CALLBACK;
	}
	
	/**
	 * 删除站点
	 * @return String 
	 */
	public String delete() {
		
		Site site = siteService.find(id);
		if (site == null) {
			addActionError("站点不存在无法删除.");
		} else {
			if (!site.getIsClose()) {
				addActionError("站点处于开放状态,无法删除.如需删除请先关闭站点.");
			} else {
				addActionMessage("站点已删除.");
				siteService.delete(id);
			}
		}
		return CALLBACK;
	}
	
	/**
	 * 站点启用 
	 * @return String
	 */
	public String start() {
		
		Site site = siteService.find(id);
		if (site != null && site.getIsClose()) {
			siteService.open(id);
		}
		return CALLBACK;
	}
	
	/**
	 * 站点禁用
	 * @return String
	 */
	public String stop() {
		Site site = siteService.find(id);
		if (site != null && !site.getIsClose()) {
			siteService.close(id);
		}
		return CALLBACK;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Site getSite() {
		return site;
	}

	public void setSite(Site site) {
		this.site = site;
	}

	public List<Site> getSiteList() {
		return siteList;
	}

	public List<WebCacheModel> getWebCacheModelList() {
		return webCacheModelList;
	}

	public void setWebCacheModelList(List<WebCacheModel> webCacheModelList) {
		this.webCacheModelList = webCacheModelList;
	}

	public List<SiteGroup> getSiteGroupList() {
		return siteGroupList;
	}

	public void setSiteService(SiteService siteService) {
		this.siteService = siteService;
	}

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	public void setSiteFunctionService(SiteFunctionService siteFunctionService) {
		this.siteFunctionService = siteFunctionService;
	}

	public void setSiteGroupService(SiteGroupService siteGroupService) {
		this.siteGroupService = siteGroupService;
	}
}
